/*
 * Copyright 2011-2012 Rivoli team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.rivoli.model.support

import com.googlecode.rivoli.RivoliException
import com.googlecode.rivoli.model.Element
import com.googlecode.rivoli.model.Model

import org.testng.annotations.Test

class ModelSupportTest {
	@Test
	void 'new model should not have any element descriptors'() {
		Model model = new TestModel()

		assert model.elementDescriptors.empty
	}

	@Test
	void 'element descriptors should be remembered'() {
		ModelSupport model = new TestModel()

		model.addElementDescriptor(new ElementDescriptorSupport('book'))

		assert model.elementDescriptors*.name == ['book']
	}

	@Test(expectedExceptions = RivoliException)
	void 'adding duplicate element descriptor should not be allowed'() {
		ModelSupport model = new TestModel()

		model.addElementDescriptor(new ElementDescriptorSupport('book'))
		model.addElementDescriptor(new ElementDescriptorSupport('book'))
	}

	@Test
	void 'creating element should be possible'() {
		ModelSupport model = new TestModel()

		model.addElementDescriptor(new ElementDescriptorSupport('book'))

		Element element = model.createElement('book')

		assert element != null
	}

	@Test(expectedExceptions = RivoliException)
	void 'creating element should fail for unknown descriptor name'() {
		ModelSupport model = new TestModel()

		model.createElement('book')
	}

	private static class TestModel extends ModelSupport {}
}